from itsdangerous import TimedJSONWebSignatureSerializer as TJWSS
from django.conf import settings


# 加密
def encode_openid(openid):
    s = TJWSS(secret_key=settings.SECRET_KEY, expires_in=300)
    signature_data = s.dumps({"openid": openid}).decode()

    return signature_data


# 解密
def decode_openid(data):
    s = TJWSS(secret_key=settings.SECRET_KEY, expires_in=300)
    try:
        decode_data = s.loads(data)
    except Exception as e:
        print(e)
        return None
    openid = decode_data.get('openid')
    return openid
